package com.devunion.bogdan.view.actions;

import com.devunion.bogdan.view.project.actions.ProjectActionFactory;
import com.devunion.bogdan.view.project.actions.ProjectOpenAction;
import com.devunion.bogdan.view.project.actions.ProjectReopenAction;
import com.devunion.bogdan.view.actions.annotations.*;
import com.devunion.bogdan.view.editors.SelectionManager;
import com.devunion.bogdan.view.editors.AnnotationManager;
import com.devunion.bogdan.view.editors.AnnotationView;
import com.devunion.bogdan.view.editors.event.SelectionListener;
import com.devunion.bogdan.model.project.ProjectManager;
import com.devunion.bogdan.model.project.UserRole;
import com.devunion.bogdan.model.project.User;
import com.devunion.bogdan.model.project.Annotation;
import com.devunion.bogdan.model.event.ProjectListener;
import com.devunion.bogdan.model.event.ProjectEvent;
import com.devunion.bogdan.model.event.UserListener;
import com.devunion.bogdan.model.event.UserEvent;
import com.devunion.bogdan.model.UserManager;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;

/**
 * @author Viktar Liaskovich
 */
public class ActionManager {
    private static ActionManager instance = new ActionManager();

    public static ActionManager getInstance() {
        return instance;
    }

    private ActionManager() {
//        ProjectManager.getInstance().addProjectListener(new ProjectListener() {
//            public void projectClosed(ProjectEvent e) {
//                update(false);
//            }
//
//            public void projectCreated(ProjectEvent e) {
//                update(true);
//            }
//        });

        UserManager.getInstance().addUserListener(new UserListener() {
            public void userLoggedIn(UserEvent userEvent) {
                updateImpl(ProjectManager.getInstance().isProjectOpened(), userEvent.getUser());
            }
        });

        final SelectionManager selectionManager = AnnotationManager.getInstance().getSelectionManager();
        selectionManager.addSelectionListener(new SelectionListener() {
            public void onSelectionChange(Component c, boolean selection) {
                final User user = UserManager.getInstance().getUser();
                List<Component> components = selectionManager.getSelectedComponents();

                if (user != null) {
                    if (user.isAdmin()) {
                        if (components.size() > 0) {
                            removeAnnotationAction.setEnabled(true);
                        } else {
                            removeAnnotationAction.setEnabled(false);
                        }
                    } else if (user.isExpert()) {

                        if (selection) {
                            boolean isOwnAnnotations = true;

                            for (Component comp : components) {
                                Annotation annotation = ((AnnotationView) comp).getAnnotation();

                                if (!annotation.getOwner().getLogin().equals(user.getLogin())) {
                                    isOwnAnnotations = false;
                                    break;
                                }
                            }

                            removeAnnotationAction.setEnabled(isOwnAnnotations);
                        } else {
                            removeAnnotationAction.setEnabled(false);
                        }
                    }
                }
            }

            public void onSelectionAllChange(boolean selection) {
                if (!selection) {
                    removeAnnotationAction.setEnabled(false);
                }
            }
        });
    }

    public final Action reopenAction = new ProjectReopenAction();
    public final Action exitAction = new FileExitAction();

    public final Action helpAction = new HelpAction();
    public final Action aboutAction = new AboutAction();

    public final Action openProjectAction = new ProjectOpenAction();
    public final Action newProjectAction = ProjectActionFactory.createAction(ProjectActionFactory.NEW_PROJECT_ACTION);
    public final Action deleteProjectAction = ProjectActionFactory.createAction(ProjectActionFactory.DELETE_PROJECT_ACTION);
    public final Action recentProjectsAction = ProjectActionFactory.createAction(ProjectActionFactory.RECENT_PROJECTS_ACTION);
    public final Action closeProjectAction = ProjectActionFactory.createAction(ProjectActionFactory.CLOSE_PROJECT_ACTION);
    public final Action saveProjectAction = ProjectActionFactory.createAction(ProjectActionFactory.SAVE_PROJECT_ACTION);

    public final Action viewProjectsBaseAction = ProjectActionFactory.createAction(ProjectActionFactory.VIEW_PROJECTS_BASE_ACTION);
    public final Action manageProjectsAction = ProjectActionFactory.createAction(ProjectActionFactory.MANAGE_PROJECTS_ACTION);

    public final Action loginDlgAction = ProjectActionFactory.createAction(ProjectActionFactory.LOGIN_DLG_ACTION);
    public final Action loginAction = ProjectActionFactory.createAction(ProjectActionFactory.LOGIN_ACTION);

    /**
     * Editor toolbar actions 
     */
    public final Action addTextAnnotationAction = new AddTextAnnotationAction();
    public final Action addImageAnnotationAction = new AddImageAnnotationAction();
    public final Action removeAnnotationAction = new RemoveAnnotationAction();
    public final Action showHideAnnotationsAction = new ShowHideAnnotationsAction();
    public final Action zoomAction = new ZoomAction();


    public void update(boolean isOpened) {
        final User user = UserManager.getInstance().getUser();

        updateImpl(isOpened, user);
    }

    private void updateImpl(boolean isOpened, User user) {
        closeProjectAction.setEnabled(isOpened);

        if (user == null || user.getRole() == UserRole.NORMAL) {
            newProjectAction.setEnabled(false);
            saveProjectAction.setEnabled(false);

            addTextAnnotationAction.setEnabled(false);
            addImageAnnotationAction.setEnabled(false);
            removeAnnotationAction.setEnabled(false);
        } else if (user.getRole() == UserRole.EXPERT) {
            newProjectAction.setEnabled(true);
            saveProjectAction.setEnabled(isOpened);

            addTextAnnotationAction.setEnabled(true);
            addImageAnnotationAction.setEnabled(true);
        } else if (user.getRole() == UserRole.ADMIN) {
            newProjectAction.setEnabled(true);
            saveProjectAction.setEnabled(isOpened);

            addTextAnnotationAction.setEnabled(true);
            addImageAnnotationAction.setEnabled(true);
        }
    }
}

